local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name);--
local UIManager = ClientOHOH.load.UIManager();
local WorkShopGui = ClientOHOH.load.UIManager():CreateUI("WorkShopGui",require(script.WorkShopGui))

local CallBackManager = ClientOHOH.load.CallBackManager();
local ConnectClass = _G.CommonOHOH.load.Connect();
local game_cfg = ClientOHOH.config.Game
local function_list = ClientOHOH.load.FunctionList();
function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent)
    self.player=self.parent
    self.DataBase=self.player:GetComponent(_G.ClientOHOH.load.Client_PlayerDataComponent())
    --self.WorkShop= ClientOHOH:GetPath_Loop("WorkShop",game.Workspace)

    local recipeData=self.DataBase:GetData("recipe")
    local carData=self.DataBase:GetData("car")
    local topData  =self.DataBase:GetData("workshop")

    WorkShopGui:SetDataBase(self.DataBase)
    WorkShopGui:UpdateBox(recipeData,carData)--装备更新在Had更新中
    WorkShopGui:UpdateTop(topData)--
    WorkShopGui:Hide()
    self.UserDataEventConnect=self.parent.UserDataEvent:Connect(function(data)
        if data["recipe"] then
            print("遗物更新",data["recipe"])
            --print("recipe changed",data["recipe"])
            WorkShopGui:UpdateBox(data["recipe"],nil)
        end
        if data["car"] then
            --print("car changed",data["car"])
            WorkShopGui:UpdateBox(nil,data["car"])
        end
        if data["workshop"] then
            WorkShopGui:UpdateTop(data["workshop"])--
        end
	end)

    WorkShopGui.RelicMakeEvent:Connect(function(rCfg)
         local result = self:Invoke(function_list.recipe_make,rCfg)
         if result then
            print("制作成功")
            self.player.CraftRelicEvent:Action()
         end
         return result
    end)
    WorkShopGui.RelicEquipEvent:Connect(function(rCfg)
        local result = self:Invoke(function_list.recipe_equip,rCfg)
        if result then
            self:ChangeRngData(rCfg,1)
        end
        return result
    end)
    WorkShopGui.RelicUnEquipEvent:Connect(function(rCfg)
        local result = self:Invoke(function_list.recipe_unequip,rCfg)
        if result then
            self:ChangeRngData(rCfg,-1)
        end
        return result
    end)
    WorkShopGui.FuseEvent:Connect(function(fuseList)
        local result = self:Invoke(function_list.part_fuse,fuseList)
        print("熔断结果",result)
        --return result
    end)

    --进入特定区域弹窗
    self:AddConnect()

end;
function M.AddConnect(self)
    local GoldenPart=ClientOHOH:GetPath_Loop("WorkShop",game.Workspace.Touch)
        -- print("child Add 1————————————————————————")
        GoldenPart.ChildAdded:Connect(function(child)
            local touchPart = child
            CallBackManager:AddRbxConnect(self,touchPart.Touched, function(obj,chPart)
                if not _G.CommonOHOH.load.GeneralTool().IsMeTouchEvent(chPart,"LeftLowerLeg") then
                    return 
                end
                WorkShopGui:Show()
            end);
        
            CallBackManager:AddRbxConnect(self,touchPart.TouchEnded, function(obj,chPart)
                if not _G.CommonOHOH.load.GeneralTool().IsMeTouchEvent(chPart,"LeftLowerLeg")  then
                    return 
                end
                WorkShopGui:Hide()
            end);
        end)
end
function M.ChangeRngData(self,rCfg,mul)
	if not self.RngDataCtrl then
        self.RngDataCtrl = self.player:GetComponent(_G.CommonOHOH.load.Common_RngDataLayer())
        print("workshop .RngDataCtrl ",self.RngDataCtrl)
    end

	for i = 1, #rCfg.property do
 		local value = rCfg.value[i] 
		local valType =rCfg.valueType[i]
		
		if valType == 1 then
			self.RngDataCtrl:AddLucky(value*mul)
		elseif valType == 2 then
			self.RngDataCtrl:AddShinyLucky(value*mul)
		elseif valType == 3 then
			self.RngDataCtrl:AddRainbowLucky(value*mul)
		elseif valType == 4 then
			self.RngDataCtrl:AddVoidLucky(value*mul)
		elseif valType == 5 then
			self.RngDataCtrl:AddRollSpeed(value*mul)
		end
	end
	
end

function M.dtor(self)
	M.super.dtor(self);
end;

return M